home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13927 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: linux.nildram.co.uk!news
  2. From: colin@greench.co.uk (Colin Wray)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is byte-alignment?
  5. Date: Wed, 10 Apr 1996 21:58:01 GMT
  6. Organization: Greenchurch Software Ltd
  7. Message-ID: <4kheda$im5@linux.nildram.co.uk>
  8. References: <4jprfe$c6q@alcor.usc.edu>
  9. NNTP-Posting-Host: ppp4.nildram.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. wawda@alcor.usc.edu (Abu Wawda) wrote:
  13.  
  14. >I often hear the phrase "byte-aligment" in this newsgroups, yet have
  15. >no clue what it is. Can someone please explain it to me? It comes up
  16. >often when people talk about structures. Thanks in advance,
  17.  
  18. Well I guess you know all about it now.
  19.  
  20. For anyone interested, you can make your code portable with the
  21. following macro:
  22.  
  23. #define ALIGN_BOUND( type) \
  24.     ( sizeof ( struct { char c; type t; } ) - sizeof ( type ) )
  25.  
  26. Then a=ALIGN_BOUND(long);  will give you the byte multiple on which
  27. the current compiler places a variable of type long. eg:
  28.  
  29. a==4  if longs must start on an address which is a multiple of 4.
  30. a==2  if longs must start on an address which is a multiple of 2.
  31.  
  32. etc. Works for any type you care to name of course. I had to use it in
  33. a library function for a database, where the caller passed the start
  34. address of his structure, and the variable types, but I had to guess
  35. the offsets for each variable.
  36.  
  37. -Colin
  38.  
  39. ----------------------------------------------------------------------------
  40. Colin Wray, Greenchurch Software Ltd, UK
  41. Email: colin@greench.co.uk
  42. ----------------------------------------------------------------------------
  43.  
  44.